var webComm = {};
//编译模板
(function($) {
	var compiled = {};
    $.fn.template7 = function(template, data) {
        if (template instanceof jQuery) {
        	var templateId = $(template).attr('id');
        }else{
        	return false;
        }
        if(typeof(compiled[templateId]) == 'undefined'){
        	template = $(template).html();
        	compiled[templateId]=Template7.compile(template);
        }
        var appendFlag = arguments[2] ? arguments[2] : 0;
        if (appendFlag) {
            this.append(compiled[templateId](data));
        } else {
            this.html(compiled[templateId](data));
        }
    };
    Template7.registerHelper('numbleIndex',function(currentPage,pageSize,index){
    	return (currentPage-1)*pageSize+index+1;
    });
    //协议规则状态背景图
	Template7.registerHelper('stateSwitch',function(activeType){
    	return activeType=="0"?'images/shix.png':'images/shengx.png';
	});
})(jQuery);
//是否定义
function isUndefined(obj) {
    return typeof(obj) == 'undefined';
}
//是否为空对象
function isNullObj(obj){
    for(var i in obj){
        if(obj.hasOwnProperty(i)){
            return false;
        }    
    }
    return true;
}
//对象转化为数组
function objToArr(obj){
    var res = [];
    if(!isNullObj(obj)){
        for(var i in obj){
            res.push(obj[i]);  
        }
    }
    return res;
}
//IP转成整型
function _ip2int(ip){
    var num = 0;
    ip = ip.split(".");
    num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]);
    num = num >>> 0;
    return num;
}
//整型解析为IP地址
function _int2iP(num){
    var str;
    var tt = new Array();
    tt[0] = (num >>> 24) >>> 0;
    tt[1] = ((num << 8) >>> 24) >>> 0;
    tt[2] = (num << 16) >>> 24;
    tt[3] = (num << 24) >>> 24;
    str = String(tt[0]) + "." + String(tt[1]) + "." + String(tt[2]) + "." + String(tt[3]);
    return str;
}
//对Date的扩展，将 Date 转化为指定格式的String
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
//例子： 
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
//(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function(fmt) { //author: meizz 
 var o = {
     "M+": this.getMonth() + 1, //月份 
     "d+": this.getDate(), //日 
     "h+": this.getHours(), //小时 
     "m+": this.getMinutes(), //分 
     "s+": this.getSeconds(), //秒 
     "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
     "S": this.getMilliseconds() //毫秒 
 };
 if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
 for (var k in o)
     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
 return fmt;
};
//10位16进制随机数+时间戳+10位16进制随机数(共36位)
function _newGuid(){
    var guid = "";
    for (var i = 1; i <= 10; i++){
    	var n = Math.floor(Math.random()*16.0).toString(16);
    	guid +=n;
    }
    guid = guid+"-"+new Date().getTime()+"-";
    for (var i = 1; i <= 10; i++){
    	var n = Math.floor(Math.random()*16.0).toString(16);
    	guid +=n;
	}
    return guid.toUpperCase();   
}

//显示提示信息
function _showPrompt(msg){
	$("#prompt_box dd").html(msg);
	$('#prompt_box').fadeIn(150);
	$('.mask').fadeIn(150);
	$('.closePromptBtn').one('click', function() { //关闭按钮
    	$('#prompt_box').fadeOut(150);
    	$('.mask').fadeOut(150);
    })
}

//确认提示框
webComm.swalConfirmAlert = function(callback){
	swal({
		title:'提示信息',
		text:'请确认真的要删除吗？',
		type:'warning',
		showCancelButton:true,
		cancelButtonColor:'#DE8020',
		confirmButtonColor:'#41740E',
		confirmButtonText:"确定",
		cancelButtonText:"取消",
		closeOnConfirm:false,
		closeOnCancel:true,
		showLoaderOnConfirm:true
	},function(isConfirm){
		if(isConfirm && typeof(callback) == "function"){
            callback();
		}
	});
};
//确认提示框
webComm.swalConfirmOptionAlert = function(text,callback){
	return swal({
		title:'提示信息',
		text:text,
		type:'warning',
		showCancelButton:true,
		cancelButtonColor:'#DE8020',
		confirmButtonColor:'#41740E',
		confirmButtonText:"确定",
		cancelButtonText:"取消",
		closeOnConfirm:false,
		closeOnCancel:true,
		showLoaderOnConfirm:true
	},function(isConfirm){
		if(isConfirm && typeof(callback) == "function"){
            callback();
		}
	});
};
//信息提示框
webComm.swalAlert = function(msg){
	var opts = {
			title:'提示信息',
			text:msg,
			html:true,
			confirmButtonText:"确定",
			confirmButtonColor:'#41740E'
		};
	var autoClose = typeof(arguments[1])!='undefined'?arguments[1]:1;
	if(autoClose){
		opts.timer = 1500;
	}
	swal(opts);
};

webComm.init = function(){
	require(['nicescroller'], function() {
		$('.nice').niceScroll({
            cursorcolor: "#fff",//#CC0071 光标颜色
            cursoropacitymax: 0.8, //改变不透明度非常光标处于活动状态（scrollabar“可见”状态），范围从1到0
            touchbehavior: false, //使光标拖动滚动像在台式电脑触摸设备
            cursorwidth: "5px", //像素光标的宽度
            cursorborder: "0", // 	游标边框css定义
            cursorborderradius: "5px",//以像素为光标边界半径
            autohidemode: false //是否隐藏滚动条
        });
	});
	//全选
	$(".communication_whitelist_con").on("change","#checkAll",function(){
		if($("#checkAll").is(":checked")){
			$('.communication_whitelist_con :checkbox').attr("checked",true);
		}else{
			$('.communication_whitelist_con :checkbox').removeAttr("checked");
		}
	}).on('change',':checkbox:not(#checkAll)',function(){
		if(!$(this).attr("checked") && $("#checkAll").is(":checked")){
			$("#checkAll").attr("checked",false);
		}
	});
	var pageJs = $("#currPage").attr('data-pageJs');
	if(typeof(pageJs)!='undefined'){
		require([pageJs],function(page){
			page.init();
		});
	}
};